import fs from 'fs';
import {config} from "../config/config";

export const getTodayDate = (): string => {
    const now = getDate();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要+1
    const day = String(now.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
};

export function delay() {
    return new Promise((resolve) => {
        setTimeout(resolve, 1000 * config.get_ecpm_delay)
    })
}

export function getDate() {
    const now = new Date();
    const millis = 8 * 60 * 60 * 1000;
    return new Date(now.getTime() + millis)
}

export function stringToDate(str_date?: string): Date {
    if (str_date === null || str_date === undefined) {
        return new Date()
    } else {
        const parts = str_date.split(/[- :]/); //分割
        const year = parts[0];
        const month = parts[1];
        const day = parts[2];
        const hour = parts[3];
        const minutes = parts[4];
        const second = parts[5];
        return new Date(`${year}-${month}-${day}T${hour}:${minutes}:${second}+08:00`);
    }
}

export function isDocker() {
    try {
        if (fs.existsSync('/.dockerenv')) {
            return true;
        }
        const cgroup = fs.readFileSync('/proc/1/cgroup', 'utf8');
        return cgroup.includes('docker');
    } catch (err) {
        return false;
    }
}